﻿using System;
using System.Windows.Controls;
using Graphe.Collations;
using Graphe.Collations.Commands;
using Graphe.Windows.Dialogs;

namespace Graphe.Windows.Controls.Collations
{
    public class CollationControlController
    {
        #region Buttons

        private Button _buttonAddSacrum;
        private Button _buttonEditSacrum;
        private Button _buttonRemoveSacrum;
        private Button _buttonAddManuscript;
        private Button _buttonEditManuscript;
        private Button _buttonRemoveManuscript;
        private Button _buttonAddVariant;
        private Button _buttonEditVariant;
        private Button _buttonRemoveVariant;
        private Button _buttonAddReading;
        private Button _buttonEditReading;
        private Button _buttonRemoveReading;
        private Button _buttonAddFragment;
        private Button _buttonEditFragment;
        private Button _buttonRemoveFragment;
        private Button _buttonAddLacunateMss;
        private Button _buttonEditLacunateMss;
        private Button _buttonRemoveLacunateMss;
        private Button _buttonAddNote;
        private Button _buttonEditNote;
        private Button _buttonRemoveNote;

        public Button ButtonAddSacrum
        {
            set
            {
                _buttonAddSacrum = value;
                _buttonAddSacrum.Click += new System.Windows.RoutedEventHandler(_buttonAddSacrum_Click);
            }
        }

        public Button ButtonEditSacrum
        {
            set
            {
                _buttonEditSacrum = value;
                _buttonEditSacrum.Click += new System.Windows.RoutedEventHandler(_buttonEditSacrum_Click);
            }
        }

        public Button ButtonRemoveSacrum
        {
            set 
            { 
                _buttonRemoveSacrum = value;
                _buttonRemoveSacrum.Click += new System.Windows.RoutedEventHandler(_buttonRemoveSacrum_Click);
            }
        }

        public Button ButtonAddManuscript
        {
            set
            {
                _buttonAddManuscript = value;
                _buttonAddManuscript.Click += new System.Windows.RoutedEventHandler(_buttonAddManuscript_Click);
            }
        }

        public Button ButtonEditManuscript
        {
            set
            {
                _buttonEditManuscript = value;
                _buttonEditManuscript.Click += new System.Windows.RoutedEventHandler(_buttonEditManuscript_Click);
            }
        }

        public Button ButtonRemoveManuscript
        {
            set
            {
                _buttonRemoveManuscript = value;
                _buttonRemoveManuscript.Click += new System.Windows.RoutedEventHandler(_buttonRemoveManuscript_Click);
            }
        }

        public Button ButtonAddVariant
        {
            set { _buttonAddVariant = value; }
        }

        public Button ButtonEditVariant
        {
            set { _buttonEditVariant = value; }
        }

        public Button ButtonRemoveVariant
        {
            set { _buttonRemoveVariant = value; }
        }

        public Button ButtonAddReading
        {
            set { _buttonAddReading = value; }
        }

        public Button ButtonEditReading
        {
            set { _buttonEditReading = value; }
        }

        public Button ButtonRemoveReading
        {
            set { _buttonRemoveReading = value; }
        }

        public Button ButtonAddFragment
        {
            set { _buttonAddFragment = value; }
        }

        public Button ButtonEditFragment
        {
            set { _buttonEditFragment = value; }
        }

        public Button ButtonRemoveFragment
        {
            set { _buttonRemoveFragment = value; }
        }

        public Button ButtonAddLacunateMss
        {
            set { _buttonAddLacunateMss = value; }
        }

        public Button ButtonEditLacunateMss
        {
            set { _buttonEditLacunateMss = value; }
        }

        public Button ButtonRemoveLacunateMss
        {
            set { _buttonRemoveLacunateMss = value; }
        }

        public Button ButtonAddNote
        {
            set { _buttonAddNote = value; }
        }

        public Button ButtonEditNote
        {
            set { _buttonEditNote = value; }
        }

        public Button ButtonRemoveNote
        {
            set { _buttonRemoveNote = value; }
        }

        #endregion
        #region Singleton

        private static CollationControlController _controller;

        public static CollationControlController Instance
        {
            get
            {
                if (_controller == null)
                    _controller = new CollationControlController();

                return _controller;
            }
        }

        #endregion



        private ICollationControl _selectedICollationControl;
        
        public Collation Collation { get; set; }

        public ICollationControl SelectedICollationControl
        {
            get
            {
                return _selectedICollationControl;
            }
            set
            {
                if (_selectedICollationControl != null)
                    _selectedICollationControl.Selected = false;

                _selectedICollationControl = value;
                _selectedICollationControl.Selected = true;

                if (_selectedICollationControl is CollationTextBlock)
                {
                    CollationTextBlock textBlock = _selectedICollationControl as CollationTextBlock;

                    if (textBlock.Tag is Manuscript)
                        ManuscriptListSelected();
                    else if (textBlock.Tag is Sacrum)
                        SacrumControlSelected();
                }
                else if (_selectedICollationControl is SacrumControl)
                {
                    SacrumControlSelected();
                }
            }
        }

        #region Manuscript Logic

        private void ManuscriptListSelected()
        {
            HideAllButtons();

            _buttonAddManuscript.Visibility = System.Windows.Visibility.Visible;

            if (((_selectedICollationControl as CollationTextBlock).Tag) as Manuscript != null)
            {
                _buttonRemoveManuscript.Visibility = System.Windows.Visibility.Visible;
                _buttonEditManuscript.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void _buttonAddManuscript_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            AddManuscriptDialog dlg = new AddManuscriptDialog();
            if ((bool)dlg.ShowDialog() == true)
            {
                CollationTextBlock textBlock = _selectedICollationControl as CollationTextBlock;
                ManuscriptListControl listControl = textBlock.Parent as ManuscriptListControl;
                AddToCollectionCommand<Manuscript> cmd = new AddToCollectionCommand<Manuscript>(new Manuscript(dlg.Manuscript, dlg.Signifier), listControl.Manuscripts);
                Collation.Do(cmd);
                listControl.Render();
            }
        }

        private void _buttonEditManuscript_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            AddManuscriptDialog dlg = new AddManuscriptDialog();
            CollationTextBlock textBlock = _selectedICollationControl as CollationTextBlock;
            ManuscriptListControl listControl = textBlock.Parent as ManuscriptListControl;
            Manuscript manuscript = textBlock.Tag as Manuscript;
            dlg.Manuscript = manuscript.Symbol;
            dlg.Signifier = manuscript.Signifier;
            if ((bool)dlg.ShowDialog() == true)
            {
                EditManuscriptCommand cmd = new EditManuscriptCommand(dlg.Manuscript, dlg.Signifier, manuscript);
                Collation.Do(cmd);
                listControl.Render();
            }
        }

        private void _buttonRemoveManuscript_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CollationTextBlock textBlock = _selectedICollationControl as CollationTextBlock;
            ManuscriptListControl listControl = textBlock.Parent as ManuscriptListControl;
            RemoveManuscriptCommand cmd = new RemoveManuscriptCommand(textBlock.Tag as Manuscript, listControl.Manuscripts);
            Collation.Do(cmd);
            listControl.Render();
        }

        #endregion
        #region Sacrum Logic


        private void SacrumControlSelected()
        {
            HideAllButtons();

            _buttonAddSacrum.Visibility = System.Windows.Visibility.Visible;
            _buttonEditSacrum.Visibility = System.Windows.Visibility.Visible;
            _buttonRemoveSacrum.Visibility = System.Windows.Visibility.Visible;
        }

        public void InvokeEditSacrumDialog()
        {
            EditNominaSacraDialog dlg = new EditNominaSacraDialog();
            dlg.Title = "Editing Sacrum";
            if ((bool)dlg.ShowDialog() == true)
            {
            }
        }


        private void _buttonAddSacrum_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            AddSacrumDialog dlg = new AddSacrumDialog();
            dlg.Title = "Add New Sacrum";
            if ((bool)dlg.ShowDialog() == true)
            {
                SacrumControl sacrumControl = _selectedICollationControl as SacrumControl;
                NominaSacraControl sacraControl = (sacrumControl.Parent as StackPanel).Parent as NominaSacraControl;

                Sacrum sacrum = new Sacrum();
                sacrum.Text = dlg.Text;
                sacrum.Representation = dlg.Representation;
                sacrum.Manuscripts = new ManuscriptCollection();

                Manuscript manuscript = new Manuscript();
                manuscript.Symbol = dlg.ManuscriptSymbol;
                manuscript.Signifier = dlg.ManuscriptSignifier;
                sacrum.Manuscripts.Add(manuscript);
                
                AddToCollectionCommand<Sacrum> cmd = new AddToCollectionCommand<Sacrum>(sacrum, sacraControl.NominaSacra);
                Collation.Do(cmd);
                sacraControl.Render();
            }
        }

        private void _buttonEditSacrum_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            EditNominaSacraDialog dlg = new EditNominaSacraDialog();
            SacrumControl sacrumControl = (_selectedICollationControl as CollationTextBlock).Parent as SacrumControl;
            Sacrum sacrum = sacrumControl.Sacrum;
            dlg.Text = sacrum.Text;
            dlg.Representation = sacrum.Representation;

            if ((bool)dlg.ShowDialog() == true)
            {

                EditSacrumCommand cmd = new EditSacrumCommand(sacrum, dlg.Text, dlg.Representation);
                Collation.Do(cmd);
                sacrumControl.Render();
            }
        }

        private void _buttonRemoveSacrum_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SacrumControl sacrumControl = _selectedICollationControl as SacrumControl;
            NominaSacraControl sacraControl = (sacrumControl.Parent as StackPanel).Parent as NominaSacraControl;
            Sacrum sacrum = sacrumControl.Sacrum;
            RemoveFromCollectionCommand<Sacrum> cmd = new RemoveFromCollectionCommand<Sacrum>(sacrum, sacraControl.NominaSacra);
            Collation.Do(cmd);
            sacraControl.Render();
        }



        #endregion

        public void HideAllButtons()
        {
             _buttonAddSacrum.Visibility = System.Windows.Visibility.Collapsed;
             _buttonEditSacrum.Visibility = System.Windows.Visibility.Collapsed;
             _buttonRemoveSacrum.Visibility = System.Windows.Visibility.Collapsed;
             _buttonAddManuscript.Visibility = System.Windows.Visibility.Collapsed;
             _buttonEditManuscript.Visibility = System.Windows.Visibility.Collapsed;
             _buttonRemoveManuscript.Visibility = System.Windows.Visibility.Collapsed;
             _buttonAddVariant.Visibility = System.Windows.Visibility.Collapsed;
             _buttonEditVariant.Visibility = System.Windows.Visibility.Collapsed;
             _buttonRemoveVariant.Visibility = System.Windows.Visibility.Collapsed;
             _buttonAddReading.Visibility = System.Windows.Visibility.Collapsed;
             _buttonEditReading.Visibility = System.Windows.Visibility.Collapsed;
             _buttonRemoveReading.Visibility = System.Windows.Visibility.Collapsed;
             _buttonAddFragment.Visibility = System.Windows.Visibility.Collapsed;
             _buttonEditFragment.Visibility = System.Windows.Visibility.Collapsed;
             _buttonRemoveFragment.Visibility = System.Windows.Visibility.Collapsed;
             _buttonAddLacunateMss.Visibility = System.Windows.Visibility.Collapsed;
             _buttonEditLacunateMss.Visibility = System.Windows.Visibility.Collapsed;
             _buttonRemoveLacunateMss.Visibility = System.Windows.Visibility.Collapsed;
             _buttonAddNote.Visibility = System.Windows.Visibility.Collapsed;
             _buttonEditNote.Visibility = System.Windows.Visibility.Collapsed;
             _buttonRemoveNote.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
